/**
 * @file yuv4.c
 * @author your name (you@domain.com)
 * @brief YUV420P像素数据 文件的亮度减半
 * @version 0.1
 * @date 2021-04-27
 * 
 * @copyright Copyright (c) 2021
 * 
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

int simplest_yuv420_halfy(char * url, int w, int h, int num);

int main(void)
{
    simplest_yuv420_halfy("../resource/sintel_640_360.yuv", 640, 360, 100);
    return 0;
}

int simplest_yuv420_halfy(char * url, int w, int h, int num)
{
    FILE * fp = fopen(url, "rb+");
    if(fp == NULL) {
        printf("fopen failed[%s] \n", strerror(errno));
        return -1;
    }
    FILE *fp1 = fopen("output_half.yuv", "wb+"); 

    uint8_t * pic = (uint8_t *)malloc(w * h * 3 / 2);

    for (int i = 0; i < num; i++)
    {
        fread(pic, 1, w * h * 3 / 2, fp);

        for (int j = 0; j < w * h; j++)//w *h 这是 Y 的size
        {
            uint8_t temp = pic[j] / 2;
            pic[j] = temp;
        }
        fwrite(pic, 1, w * h * 3 / 2, fp1);
    }

    free(pic);
    fclose(fp);
    fclose(fp1);   

    return 0;
}
